package edu.sctu;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author:Mrzhang
 * @Description:
 * @Date:2018.05.15 16:43
 **/
public class DataBase {
    public static List<User> getUserList() throws SQLException {

       List<User> userList=new ArrayList<User>();


        Connection connection=null;
        Statement statement=null;
        ResultSet resultSet=null;

        String sql="select * from t_users";

        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        connection= DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/sctu_javaweb_2018?useUnicode=true&characterEncoding=GBK",
                "root",
                "123456"
        );
        statement=connection.createStatement();

        resultSet=statement.executeQuery(sql);

        while (resultSet.next()){
            int id=resultSet.getInt(1);
            String name=resultSet.getString(2);
            String password=resultSet.getString(3);

            //封装在user中
            User user=new User();
            user.setId(id);
            user.setName(name);
            user.setPassword(password);

            userList.add(user);


        }
        resultSet.close();
        statement.close();
        connection.close();
        //pageContext.setAttribute("userList",userList);
        return userList;
    }

}
